<template>
  <!-- 这是招聘广告的页面 -->
  <div class="Adver-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" icon="el-icon-edit" @click="handleAdd('News')">
          添加
        </el-button>
        <el-input v-model="search" placeholder="标题" style="width: 300px;padding-left:20px" />
      </div>
      <el-table
        :data="AdvertiseList.filter(data => !search || data.title.toLowerCase().includes(search.toLowerCase()))"
        style="width: 100%"
        class="list-table"
      >
        <el-table-column
          prop="recruitmentId"
          label="ID"
          width="130"
          align="center"
        />
        <el-table-column
          prop="title"
          label="标题"
          width="400"
          align="center"
        />
        <!-- <el-table-column
          prop="content"
          label="内容"
          width="500"
          align="center"
        /> -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleClick(scope.row.recruitmentId)"
            >查看</el-button>
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.recruitmentId)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- <div class="block">
        <el-pagination
          layout="prev, pager, next"
          :total="totalpages"
          background
          @current-change="onCurrentChange"
        />
      </div> -->
    </el-card>

    <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogForm">
      <el-form :model="form">
        <el-form-item label="广告ID" :label-width="formLabelWidth">
          <el-input v-model="Advs.recruitmentId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="标题" :label-width="formLabelWidth">
          <el-input v-model="Advs.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="内容" :label-width="formLabelWidth">
          <el-input v-model="Advs.content" autocomplete="off" type="textarea" :rows="10" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="handleCancel('Advs')">取 消</el-button>
        <el-button type="primary" @click.native="handleUpdate('Advs')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 调用接口
import {
  getRecruitment,
  addRecruitment,
  deleteRecruitment,
  modifyRecruitment
} from '@/api/adver'

export default {
  data() {
    return {
      // 获取的信息
      AdvertiseList: [{
        recruitmentId: '', // 广告ID
        title: '', // 标题
        content: '' // 内容
      }],
      // 上传的信息
      Advs: {
        recruitmentId: '',
        title: '',
        content: ''
      },
      search: '', // 查询的结果
      totalpages: 0, // 总页数
      dialogForm: false,
      titleMap: {
        add: '新增招聘广告',
        edit: '编辑招聘广告'
      },
      dialogStatus: '',
      flag: 0
      // contentLink: 'http://192.168.2.226:8080/news/undefined?news_id='
    }
  },
  created() {
    this.loadAdvertisement(1)
    this.flag = 0
  },
  methods: {
    // 获取广告信息
    loadAdvertisement(page) {
      getRecruitment({
        current: page,
        size: 10
      }).then(res => {
        console.log(res)
        this.AdvertiseList = res.data
        this.totalpages = res.data[0].pages * 10
      })
    },
    onCurrentChange(page) {
      this.loadAdvertisement(page)
    },
    // handleClick(recruitmentId) {
    //   console.log(recruitmentId)
    //   this.contentLink = 'http://192.168.2.226:8080/news/undefined?news_id=' + recruitmentId
    //   console.log(this.contentLink)
    // },
    // 获取添加广告的信息
    handleAdd: function(Advs) {
      this.dialogForm = true
      this.dialogStatus = 'add'
      this.flag = 1
      this.Advs.recruitmentId = Advs.recruitmentId // 这样写的原因是不和"编辑"冲突,等待以后更好的解决办法
      this.Advs.title = Advs.title
      this.Advs.content = Advs.content
    },
    handleDelete: function(recruitmentId) {
      // 删除广告
      this.$confirm('确认删除?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认执行
        console.log(recruitmentId)
        new Promise((resolve, reject) => {
          deleteRecruitment({ recruitmentId: recruitmentId }).then(res => {
          // 删除成功
            this.loadAdvertisement(this.page)
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑广告
    handleEdit: function(index, row) {
      console.log(row)
      this.dialogForm = true
      this.Advs = Object.assign({}, row)
      this.dialogStatus = 'edit'
      this.flag = 0
    },
    // 更新信息
    handleUpdate(forName) {
      if (this.flag === 1) {
        addRecruitment(this.Advs).then(res => {
          console.log(this.Advs)
          console.log(res)
          this.loadAdvertisement()
        })
        this.$message({
          type: '',
          message: '创建成功'
        })
      } else if (this.flag === 0) {
        modifyRecruitment(this.Advs).then(res => {
          console.log(res)
          this.loadAdvertisement()
        })
        this.$message({
          type: 'success',
          message: '修改成功'
        })
      }
      this.dialogForm = false
    },
    handleCancel(formName) {
      this.$message({
        type: 'info',
        message: '已取消'
      })
      this.dialogForm = false
    }
  }
}
</script>

<style>
.box-card {
  height: 850px;
}
.list-table {
  margin-bottom: 10px;
}
</style>
